package com.smart.seata.stock.mapper;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.smart.seata.stock.entity.Stock;

public interface StockMapper extends BaseMapper<Stock> {
    /**
     * 通过商品ID减少库存
     *
     * @return
     */
    default int updateQuantityByProductId(Long productId, Long quantity) {
        QueryWrapper<Stock> qw = new QueryWrapper<>();
        qw.eq(Stock.COL_PRODUCT_ID, productId);
        Stock stock = this.selectOne(qw);
        stock.setQuantity(stock.getQuantity() - quantity.intValue());
        return this.update(stock, null);
    }

}